package engine.newUi
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.display.Story.StoryMoveUI;
	import engine.display.newAttack.AttackLogic;
	import engine.display.newAttack.vo.WarfareVo;
	import engine.flag.GameDefine;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	
	/**
	 *通用结算界面逻辑 
	 * @author liuel
	 * 
	 */	
	public class CurrencyResultLogic
	{
		private static var _self		:CurrencyResultLogic;
		private var _ui				:CurrencyResultUI;
		private var _model				:ModelLocator
		
		public  var type:int //当前界面类型 2 胜利 3 失败 4 副本战斗失败   // 8 9录像界面
		private var _leaveFunc			:Function;
		private var _againFunc			:Function;
		private var _copyAddressFunc	:Function ;
		
		//自动关闭倒计时
		private var _time:Timer;
		private var _currTime:int=0;
		
		private const TOTAL_TIME:int=3;
		/**
		 *是否自动离开 
		 */		
		private var _isAuto:Boolean=false;
		public static function getInstance():CurrencyResultLogic
		{
			if(_self ==null)
			{
				_self = new CurrencyResultLogic();
			}
			return _self;
		}
		public function CurrencyResultLogic()
		{
		}
		public function  Init():void
		{
			_time	= new Timer(1000);
			_ui 	= CurrencyResultUI.getInstance();
			_model 	=ModelLocator.getInstance();
		}
		public function	BeforeShow():void
		{
			automatismLeave(_isAuto);
			addEvent();
		}
		public function	AfterHide():void
		{
			_isAuto = false;
			removeEvent();
		}
		private function onPlayTimer(e:TimerEvent):void
		{
			
			_currTime+=1;	
			var residualTime:int =TOTAL_TIME-_currTime
			_ui.okBtn.label =StaticIni.getSwfLangStr("CastleTrain_Button_OK")+residualTime+StaticIni.getSwfLangStr("Common_Second");
			if(_currTime == TOTAL_TIME)
			{
				_time.reset();
				_leaveFunc();
				_ui.hide();
				if(CampBattleLogic.getInstance().isJoinCamp)
				{
					if(!CampBattleUI.getInstance().visible)
					{
						CampBattleUI.getInstance().visible=true;
					}
				}
				/**请求是否有需要在场景加载完成后自动打开的界面*/
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHECK_SEND_WINDOW);
				
			}
		}
		public function set AutoLeave(val:Boolean):void
		{
			_isAuto = val;
		}
		/**
		 *开启自动离开战斗 
		 * 
		 */		
		private function automatismLeave(isAuto:Boolean =false):void
		{
			if(isAuto == true)
			{
				_currTime=0;
				_ui.okBtn.label =StaticIni.getSwfLangStr("CastleTrain_Button_OK")+TOTAL_TIME+StaticIni.getSwfLangStr("Common_Second");
				_time.start();
				
			}
			else
			{
				_ui.okBtn.label =StaticIni.getSwfLangStr("CastleTrain_Button_OK");
			}
			
		}
		/**
		 * 设置界面类型 
		 * @param type  1 阵营战战报界面
		 *              2 阵营战胜利界面
		 *              3 阵营战失败界面
		 *              4 副本失败界面
		 * 				5 无量玉壁结算
		 */		
		private function setMc(type:int):void
		{
			this.type=type;
			if(_ui.mc)
			{
				var x:Number=_ui.mc.x;
				var y:Number=_ui.mc.y;
			}
			
			var skin:String;
			switch(type)
			{
				
				case 1:
					skin ="";
					break;
				case 2:
				case 8:
				case 5:
				case 6:
					x =87;
					y =17;
					skin ="CustomControl_016";
					break;
				case 3:
				case 9:
				case 7:
					x =87;
					y =17;
					skin ="CustomControl_017";
					break;
				case 4:
					skin ="CustomControl_018";
					x = 50;
					y = 35;
					break;
				case 10:
					skin = "";
					x = 50;
					y = 20;
					break;
			}
			
			if(_ui.mc)
			{
				var par:DisplayObjectContainer	=	_ui.mc.parent;
				par.removeChild(_ui.mc);
			}
			_ui.mc	=	ResManager.newResClass(skin) as MovieClip;
			if(_ui.mc == null)
			{
				return;
			}
			par.addChild(_ui.mc);
			_ui.mc.x = x;
			_ui.mc.y = y;
		}
		/**
		 * 设置回调函数 
		 * @param leaveFunc				离开函数
		 * @param againFightFunc		再来一次函数
		 * @param copyAddressFunc		复制地址函数
		 */		
		public function setFunc( leaveFunc : Function, againFightFunc : Function, copyAddressFunc : Function = null):void
		{
			_leaveFunc			= leaveFunc;
			_againFunc 			= againFightFunc
			_copyAddressFunc	= copyAddressFunc;
		}
		private function addEvent():void
		{
			_ui.addEventListener(MouseEvent.CLICK,onClickBtn);
			_time.addEventListener(TimerEvent.TIMER,onPlayTimer);
			_ui.btn1.addEventListener(MouseEvent.MOUSE_OVER,onShowTips);
			_ui.btn2.addEventListener(MouseEvent.MOUSE_OVER,onShowTips);
			_ui.btn3.addEventListener(MouseEvent.MOUSE_OVER,onShowTips);
			
			_ui.btn1.addEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.btn2.addEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.btn3.addEventListener(MouseEvent.MOUSE_OUT,onHideTips);
		}
		private function removeEvent():void
		{
			_time.removeEventListener(TimerEvent.TIMER,onPlayTimer);
			_ui.removeEventListener(MouseEvent.CLICK,onClickBtn)
			
			_ui.btn1.removeEventListener(MouseEvent.MOUSE_OVER,onShowTips);
			_ui.btn2.removeEventListener(MouseEvent.MOUSE_OVER,onShowTips);
			_ui.btn3.removeEventListener(MouseEvent.MOUSE_OVER,onShowTips);
			
			_ui.btn1.removeEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.btn2.removeEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.btn3.removeEventListener(MouseEvent.MOUSE_OUT,onHideTips);
		}
		private function onClickBtn(e:MouseEvent):void
		{
			switch(e.target)
			{
				case _ui.btn1:
					break;
				case _ui.btn2:
					_copyAddressFunc();
					break;
				case _ui.btn3:
					_againFunc();
					break;
				default:
					if( GameData.SceneType == GameDefine.SceneType_ForbiddenAreaScene )
					{
						GameData.Role.resetPoint();
					}
					if(AttackLogic.getInstance().attackVo.attackType !=5 && AttackLogic.getInstance().attackVo.attackType != 6 )
					{
						UpNoticeLogic.getInstance().onAttackOver();
					}
					_leaveFunc();
					if(CampBattleLogic.getInstance().isJoinCamp)
					{
						if(!CampBattleUI.getInstance().visible)
						{
							CampBattleUI.getInstance().visible=true;
						}
					}
					/**请求是否有需要在场景加载完成后自动打开的界面*/
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHECK_SEND_WINDOW);
					break;
			}
			_ui.hide();
			
			_ui.graphics.clear();
		}
		private function onShowTips(e:MouseEvent):void
		{
			var strID:String;
			switch(e.currentTarget)
			{
				case _ui.btn1:
					strID = "UIString_1000043";
					break;
				case _ui.btn2:
					strID = "UIString_1000042";
					break;
				case _ui.btn3:
					strID = "UIString_1000041";
					break;
			}
			var tips:String = StaticIni.getSwfLangStr(strID);
			TipsMainLogic.instance.showString(tips);
		}
		private function onHideTips(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		/**
		 *添加蒙板 
		 * 
		 */		
		private function addPanle():void
		{
			_ui.graphics.beginFill(0xfff111,0.0);
			var p:Point=new Point(0,0);
			p=_ui.globalToLocal(p);
			_ui.graphics.drawRect(p.x,p.y,StageInfo.stage.stageWidth,StageInfo.stage.stageHeight);
			_ui.graphics.endFill();
		}
		/**
		 * 根据类型显示结算界面 
		 * @param type  // 1副本PVE结算 2 普通PVE胜利结算 3 PVP结算 4 无量玉壁结算 5 竞技场别的结算 6 跨服战结算
		 * @param info  // 结算数据
		 * @param win   // 结算的结果
		 * 
		 */		
		public function resultInfo( type:int,info : Array, win : Boolean ):void
		{
			//如果在剧情中，不显示奖励   这个是专门为剧情战报做的
			if(StoryMoveUI.IsStory)
			{
				onClickBtn(new MouseEvent(""));
				return ;
			}
			
			var skintype:int 	= 	type;//显示界面的类型  1 阵营战战报界面 
			if(type == 1 &&win ==false)
			{
				skintype = 4;//4 副本失败界面
			}
			else if(type == 2 &&win ==false)   //普通场景PVE失败
			{
				skintype = 7;
			}
			else if(type == 2 &&win ==true)	 //普通场景PVE胜利
			{
				skintype = 6;
			}
			else if(type ==3 &&win ==true)
			{
				skintype = 2;//2 阵营战胜利界面
			}
			else if(type == 3 && win ==false)
			{
				skintype = 3;//3 阵营战失败界面 
			}
			else if(type == 4 && win == true)
			{
				skintype = 5;
			}
			else if(type == 5 )
			{
				if(info[1] ==_model.scene.Role.getPlayerName())
					skintype =8;
				else 
					skintype =9;
			}else if(type == 6)
			{
				skintype = 10;//显示跨服战结算
			}
			setMc(skintype);
			if(_ui.created == false)
			{
				_ui.showOrHide();
			}
			_ui.label.htmlText ="";
			setStr(skintype,info)
		}
		
		private function setStr(uitype:int,arr:Array):void
		{
			_ui.label.x =116;
			_ui.label.y =93;
			switch(uitype)
			{
				//PVP 战报
				case 1:
					showWar(arr);
					break;
				//pvp 结算   赢
				case 2:
					showCampResult(arr);
					break;
				//pvp 结算   输
				case 3:
					showCampResult(arr);
					break;
				//PVE  副本失败
				case 4:
					showActionFila();
					break;
				case 5://无量玉壁结算
					showWlybResult(arr);
					break;
				case 6:
				case 7:
					_ui.label.htmlText	=	""
					break;
				case 9:
				case 8:  //竞技场他人战报
					_ui.label.x =130;
					showPkWar(arr,uitype)
					break;
				case 10:
					_ui.label.x =80;
					_ui.label.y = 50;
					showPkWar(arr,uitype)
					break;
			}
		}
		/**
		 *他人竞技场战报 
		 * @param arr
		 * 
		 */		
		private function showPkWar(arr:Array,uitype:int):void
		{
			if(uitype == 10)
			{
				arr.shift();
				_ui.label.htmlText =  StaticIni.getSwfLangStrVar("UIString_1000491",arr);
				return;
			}
			arr.shift();
			_ui.label.htmlText =  StaticIni.getSwfLangStrVar("UIString_1000491",arr);
		}
		/**
		 * 显示一条战报 
		 * 
		 */		
		private function showWar(arr:Array):void
		{
			//战报类型
			var type :int= arr[0];
			var vo:WarfareVo =CampBattleLogic.getInstance().getWarfare(type,arr)
			if(vo.name == "" || vo.name1 == "")return;
			_ui.label.htmlText = StaticIni.getSwfLangStrVar("UIString_1000068",[vo.name,vo.name1]);
		}
		
		/**
		 *无量玉壁结算 
		 * @param arr
		 * 
		 */
		private function showWlybResult(arr:Array):void
		{
			arr.shift();
			if(arr.length == 2)
			{
				_ui.label.htmlText = StaticIni.getSwfLangStrVar("UIString_1000391",[arr[1]]);
			}else if(arr.length == 4)
			{
				_ui.label.htmlText = StaticIni.getSwfLangStrVar("UIString_1000390",[arr[1],arr[3]]);
			}
			//			var str:String ="";
			//			for(var i:int = 0;i < arr.length;i+=2)
			//			{
			//				if(arr[i+1] >0 )
			//				{
			//					str += ( StaticIni.getSwfLangStr("Capital_" + arr[i]) + StaticIni.getSwfLangStrVar("UIString_1000386",[arr[i+1]] ));
			//				}
			//			}
			//			_ui.label.htmlText = str;
			//			_ui.label.htmlText = StaticIni.getSwfLangStrVar("UIString_1000069",[arr[1],arr[3]]);
		}
		
		/**
		 *显示一条阵营战结算数据 
		 *  1：铜钱  val:值  2：声望 val:值
		 */		
		private function showCampResult(arr:Array):void
		{
			if(arr.length < 3 )
			{
				_ui.label.htmlText	=	""
			}
			else
			{
				var strID:String ="";
				if(AttackLogic.getInstance().AttackSceneModel =="12320100" ||GameData.SceneConfig == 11330100)  //阵营战场景声望或者世界BOSS声望上限
				{
					strID =(arr[4] ==0)?"UIString_1000477":"UIString_1000069"
				}
				else
				{
					strID ="UIString_1000069";
				}
				_ui.label.htmlText = StaticIni.getSwfLangStrVar(strID,[arr[2],arr[4]]);
			}
			
		}
		/**
		 *用于副本失败战斗结算(不提示) 
		 * 
		 */		
		private function showActionFila():void
		{
			_ui.label.text = "";
			
		}
		
	}
}